import Vue from 'vue'
import Vuex from 'vuex'
import login from './modules/login'

Vue.use(Vuex)
const saveCongig = [
  {
    mutationName: 'LOGIN_',
    stateName: 'login',
    sessionName: '$_login'
  },
  {
    mutationName: 'DISCUSS_',
    stateName: 'roastTalking',
    sessionName: '$_roastTalking'
  },
  {
    mutationName: 'GIFT_',
    stateName: 'gift',
    sessionName: '$_gift'
  }
]

const saveToSessionStorage = (store) => {
  store.subscribe((mutation, state) => {
    for (let index = 0; index < saveCongig.length; index++) {
      const item = saveCongig[index]
      if (mutation.type.startsWith(item.mutationName)) {
        sessionStorage.setItem(item.sessionName, JSON.stringify(state[item.stateName]))
      }
    }
  })
}

export default new Vuex.Store({
  state: {},
  mutations: {},
  actions: {},
  modules: { login },
  plugins: [saveToSessionStorage]
})
